package com.misty.encryption_decryption.RSA;

import com.misty.encryption_decryption.base64.Base64Decoder;
import com.misty.encryption_decryption.base64.Base64Encoder;
import org.junit.Test;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class Demo {

    @Test
    public void demo() throws Exception {
        // 1.获取要加密的数据
        String jsonData = "iMistyimport org.junit.Test;\n" +
                "\n" +
                "import java.security.KeyPair;\n" +
                "import java.security.interfaces.RSAPrivateKey;\n" +
                "import java.security.interfaces.RSAPublicKey;\n" +
                "\n" +
                "public class Demo {\n" +
                "\n" +
                "    @Test\n" +
                "    public void demo() throws Exception {\n" +
                "        // 1.获取要加密的数据\n" +
                "        String jsonData = \"iMisty\";\n" +
                "\n" +
                "        // 2.生成秘钥对\n" +
                "        KeyPair keyPair = RSAUtil.generateRSAKeyPair(RSAUtil.DEFAULT_KEY_SIZE);\n" +
                "        // 公钥\n" +
                "        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();\n" +
                "        // 私钥\n" +
                "        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();\n" +
                "\n" +
                "\n" +
                "        //3.分别使用公钥加密 私钥解密   私钥加密 公钥解密\n" +
                "\n" +
                "        //公钥加密\n" +
                "        long start=System.currentTimeMillis();\n" +
                "        byte[] encryptBytes=    RSAUtil.encryptByPublicKeyForSpilt(jsonData.getBytes(),publicKey.getEncoded());\n" +
                "        long end=System.currentTimeMillis();\n" +
                "        System.out.println(\"MainActivity\"+\"公钥加密耗时 cost time---->\"+(end-start));\n" +
                "\n" +
                "        String encryStr= Base64Encoder.encode(encryptBytes);";

        // 2.生成秘钥对
        KeyPair keyPair = RSAUtil.generateRSAKeyPair(RSAUtil.DEFAULT_KEY_SIZE);
        // 公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        // 私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();


        //3.分别使用公钥加密 私钥解密   私钥加密 公钥解密

        //公钥加密
        long start = System.currentTimeMillis();
        byte[] encryptBytes = RSAUtil.encryptByPublicKeyForSpilt(jsonData.getBytes(), publicKey.getEncoded());
        long end = System.currentTimeMillis();
        System.out.println("MainActivity" + "公钥加密耗时 cost time---->" + (end - start));

        String encryStr = Base64Encoder.encode(encryptBytes);
        System.out.println("MainActivity" + "加密后json数据 --1-->" + encryStr);
        System.out.println("MainActivity" + "加密后json数据长度 --1-->" + encryStr.length());

        //私钥解密
        start = System.currentTimeMillis();
        byte[] decryptBytes = RSAUtil.decryptByPrivateKeyForSpilt(Base64Decoder.decode(encryStr.getBytes()), privateKey.getEncoded());
        String decryStr = new String(decryptBytes);
        end = System.currentTimeMillis();

        System.out.println("MainActivity" + "私钥解密耗时 cost time---->" + (end - start));
        System.out.println("MainActivity" + "解密后json数据 --1-->" + decryStr);

        //私钥加密
        start = System.currentTimeMillis();
        encryptBytes = RSAUtil.encryptByPrivateKeyForSpilt(jsonData.getBytes(), privateKey.getEncoded());
        end = System.currentTimeMillis();
        System.out.println("MainActivity" + "私钥加密密耗时 cost time---->" + (end - start));
        encryStr = Base64Encoder.encode(encryptBytes);
        System.out.println("MainActivity" + "加密后json数据 --2-->" + encryStr);
        System.out.println("MainActivity" + "加密后json数据长度 --2-->" + encryStr.length());
        //公钥解密
        start = System.currentTimeMillis();
        decryptBytes = RSAUtil.decryptByPublicKeyForSpilt(Base64Decoder.decode(encryStr.getBytes()), publicKey.getEncoded());
        decryStr = new String(decryptBytes);
        end = System.currentTimeMillis();
        System.out.println("MainActivity" + "公钥解密耗时 cost time---->" + (end - start));
        System.out.println("MainActivity" + "解密后json数据 --2-->" + decryStr);
    }
}
